package com.qpkj.oms.service.impl;

import java.util.Date;
import java.util.List;

import com.qpkj.common.core.domain.AjaxResult;
import com.qpkj.common.utils.DateUtils;
import com.qpkj.oms.domain.OmsTask;
import com.qpkj.oms.mapper.OmsTaskMapper;
import com.qpkj.oms.parameter.AddAchievementVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qpkj.oms.mapper.OmsAchievementMapper;
import com.qpkj.oms.domain.OmsAchievement;
import com.qpkj.oms.service.IOmsAchievementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工业绩Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@Service
public class OmsAchievementServiceImpl implements IOmsAchievementService {
    @Autowired
    private OmsAchievementMapper omsAchievementMapper;
    @Autowired
    private OmsTaskMapper taskMapper;

    /**
     * 查询员工业绩
     *
     * @param achievementId 员工业绩主键
     * @return 员工业绩
     */
    @Override
    public OmsAchievement selectOmsAchievementByAchievementId(Long achievementId) {
        return omsAchievementMapper.selectOmsAchievementByAchievementId(achievementId);
    }

    /**
     * 查询员工业绩列表
     *
     * @param omsAchievement 员工业绩
     * @return 员工业绩
     */
    @Override
    public List<OmsAchievement> selectOmsAchievementList(OmsAchievement omsAchievement) {
        return omsAchievementMapper.selectOmsAchievementList(omsAchievement);
    }

    /**
     * 新增员工业绩
     *
     * @param omsAchievement 员工业绩
     * @return 结果
     */
    @Override
    public int insertOmsAchievement(OmsAchievement omsAchievement) {
        omsAchievement.setCreateTime(DateUtils.getNowDate());
        return omsAchievementMapper.insertOmsAchievement(omsAchievement);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult addAchievement(AddAchievementVo addAchievementVo) {
        OmsTask task = taskMapper.selectOmsTaskByTaskId(addAchievementVo.getTaskId());
        if (ObjectUtils.isEmpty(task)) {
            return AjaxResult.error("任务ID不存在！");
        }
        if (ObjectUtils.isEmpty(addAchievementVo.getAchievementList())) {
            return AjaxResult.error("员工业绩不能为空！");
        }
        addAchievementVo.getAchievementList().forEach(omsAchievement -> {
            omsAchievement.setTaskId(addAchievementVo.getTaskId());
            omsAchievement.setCreateTime(new Date());
            omsAchievementMapper.insertOmsAchievement(omsAchievement);
        });
        return AjaxResult.success("员工业绩录入成功");
    }

    /**
     * 修改员工业绩
     *
     * @param omsAchievement 员工业绩
     * @return 结果
     */
    @Override
    public int updateOmsAchievement(OmsAchievement omsAchievement) {
        omsAchievement.setUpdateTime(DateUtils.getNowDate());
        return omsAchievementMapper.updateOmsAchievement(omsAchievement);
    }

    /**
     * 批量删除员工业绩
     *
     * @param achievementIds 需要删除的员工业绩主键
     * @return 结果
     */
    @Override
    public int deleteOmsAchievementByAchievementIds(Long[] achievementIds) {
        return omsAchievementMapper.deleteOmsAchievementByAchievementIds(achievementIds);
    }

    /**
     * 删除员工业绩信息
     *
     * @param achievementId 员工业绩主键
     * @return 结果
     */
    @Override
    public int deleteOmsAchievementByAchievementId(Long achievementId) {
        return omsAchievementMapper.deleteOmsAchievementByAchievementId(achievementId);
    }
}
